﻿using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using Cubicle.Core.Config;
using Cubicle.Core.Interfaces;
using Cubicle.Core.Processors;

namespace Cubicle.Core
{
    public class ComponentFactory
    {

        private static readonly NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();

        [Import] 
        private EngineConfig _config;

        [Import]
        private CompositionContainer _container;

        [Export]
        private IRepository Repository
        {
            get
            {
                _logger.Info("Resolving repository...");
                _repository = _repository ?? _container.GetExportedValue<IRepository>(_config.Repository);
                return _repository;
            }
            set
            {
                _repository = value;
            }
        }
        private IRepository _repository;

        [Export]
        private IInvoker Invoker
        {
            get
            {
                _logger.Info("Resolving Invocation Invoker...");
                _invoker = _invoker ?? new Invoker(_container, _config, _config.QueueName);
                return _invoker;

            } 
            set
            {
                _invoker = value;
            }
        }
        private IInvoker _invoker;

        //[Export]
        //private IScriptManager ScriptManager
        //{
        //    get
        //    {
        //        _scriptManager = _container.GetExportedValue<ScriptManager>();
        //        var script = _config.ScriptFile;
        //        var scriptFile = Helpers.ReadBytesFromFile(script);
        //        if (scriptFile != null)
        //        {
        //            _logger.Info("Executing script...");
        //            _scriptManager.RunByteScript(scriptFile);
        //        }
        //        return _scriptManager;
        //    }
        //    set
        //    {
        //        _scriptManager = value;
        //    }
        //}
        //private IScriptManager _scriptManager;

        [Export]
        private IHandleRegister HandleRegister
        {
            get
            {
                _logger.Info("Resolving Handle parsers...");
                _handleRegister = _container.GetExportedValue<IHandleRegister>(_config.HandleRegister);
                return _handleRegister;
            }
            set
            {
                _handleRegister = value;
            }
        }
        private IHandleRegister _handleRegister;

    }
}